package com.mdd.admin.esign;

import com.mdd.admin.esign.comm.EsignFileBean;
import com.mdd.admin.esign.comm.EsignHttpHelper;
import com.mdd.admin.esign.comm.EsignHttpResponse;
import com.mdd.admin.esign.enums.EsignHeaderConstant;
import com.mdd.admin.esign.enums.EsignRequestType;
import com.mdd.admin.esign.exception.EsignDemoException;

import java.util.Map;

/**
 * 文件服务
 * datetime 2024年1月23日下午14:19:24
 */
public class FileAnd extends Exception{
    private static String eSignHost= EsignDemoConfig.EsignHost;
    private static String eSignAppId=EsignDemoConfig.EsignAppId;
    private static String eSignAppSecret=EsignDemoConfig.EsignAppSecret;

    /**
     * 获取文件上传地址
     * @return
     */
    public static EsignHttpResponse getUploadUrl(String filePath) throws EsignDemoException {
        //自定义的文件封装类，传入文件地址可以获取文件的名称大小,文件流等数据
        EsignFileBean esignFileBean = new EsignFileBean(filePath);
        String apiaddr="/v3/files/file-upload-url";
        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm="{\n" +
                "    \"contentMd5\": \""+esignFileBean.getFileContentMD5()+"\",\n" +
                "    \"fileName\":\""+esignFileBean.getFileName()+"\"," +
                "    \"fileSize\": "+esignFileBean.getFileSize()+",\n" +
                "    \"convertToPDF\": true,\n" +
                "    \"contentType\": \""+ EsignHeaderConstant.CONTENTTYPE_STREAM.VALUE()+"\"\n" +
                "}";
        //请求方法
        EsignRequestType requestType= EsignRequestType.POST;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
    }
    /**
     *
     * 文件流上传
     * @return
     */
    public static EsignHttpResponse uploadFile(String uploadUrl, String filePath) throws EsignDemoException {
        //根据文件地址获取文件contentMd5
        EsignFileBean esignFileBean = new EsignFileBean(filePath);
        //请求方法
        EsignRequestType requestType= EsignRequestType.PUT;
        return EsignHttpHelper.doUploadHttp(uploadUrl,requestType,esignFileBean.getFileBytes(),esignFileBean.getFileContentMD5(), EsignHeaderConstant.CONTENTTYPE_STREAM.VALUE(),true);
    }

    /**
     * 获取文件上传状态
     */
    public static EsignHttpResponse getFileStatus(String fileId) throws EsignDemoException {
        String apiaddr="/v3/files/"+fileId;

        //请求参数body体,json格式。get或者delete请求时jsonString传空json:"{}"或者null
        String jsonParm=null;
        //请求方法
        EsignRequestType requestType= EsignRequestType.GET;
        //生成签名鉴权方式的的header
        Map<String, String> header = EsignHttpHelper.signAndBuildSignAndJsonHeader(eSignAppId,eSignAppSecret,jsonParm,requestType.name(),apiaddr,true);
        //发起接口请求
        return EsignHttpHelper.doCommHttp(eSignHost, apiaddr,requestType , jsonParm, header,true);
    }
}
